import httplib2 # http://bitworking.org/projects/httplib2/
from urllib import urlencode
http = httplib2.Http()

host = 'localhost:8000'
url = 'http://%s/task/' % host

params = urlencode({
    'name':'test task',
    'description':'this task is for testing'
})
response = http.request(url, 'POST', params)
headers, content = response
assert headers['status'] == '302'
location = headers['location']
task_id = int(location[:location.index('/')])
print 'created task:',task_id
print 'redirect to',location

url = 'http://%s/task/%d/' % (host, task_id)

params = urlencode({
    'name':'changed test task',
    'description':'changed this task is for testing'
})
response = http.request(url, 'PUT', params)
headers, content = response
assert headers['status'] == '302',headers['status']
print 'updated task:',task_id
print 'redirect to',headers['location']

response = http.request(url, 'GET')
headers, content = response
assert headers['status'] == '200'
print content

response = http.request(url, 'DELETE')
assert response[0]['status'] == '302'
print 'deleted task:',task_id
print 'redirect to',response[0]['location']
